package gl.training.basics;

class PrimeNumbersDemo {

	public static void main(String[] args) {
		System.out.println("***** Prime Numbers *****");
		//---------------- inputing --------------------
		int Num = -1;
		boolean repeat;
		do {		
			repeat = false;
			System.out.println("Input some integral number.");
			System.out.print("N = ");
			String strNum = "";
			try {
				byte[] buf = new byte[16];
				int q = System.in.read(buf);
				strNum = new String(buf, 0, q-2);
				Num = Integer.parseInt(strNum);			
			}catch(Exception e) {
				System.out.print("You've inputted not integer number '" +
					strNum +"'");
				System.out.println(", please try again.");
				repeat = true;
			}
		}while(repeat);		

		//--------------- calculating ------------------
		System.out.println("Prime numbers: ");
		for(int n=2; n<=Num; n++) {
			boolean n_is_prime = true;
			for(int i=2; i<=n/2; i++) {
				if(n % i == 0) {
					n_is_prime = false;
					break;
				}
			}
			if(n_is_prime) {
				System.out.print(" " + n);
			}
		}		
		System.out.println();
	}
}